home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / A.D. Software / OOFILE / Buildable, limited OOFILE / samples / ooftst12.cpp < prev    next >
C/C++ Source or Header  |  1996-03-25  |  4KB  |  125 lines

  1. // Copyright 1995 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST12
  4.  
  5. // This sample tests the compound indexes by creating a single table
  6. // and storing and retrieving indexed data.
  7.  
  8. // Simple stream I/O is used to interact with the user.
  9.  
  10. #include "oofile.hpp"
  11.  
  12. CLASS_TABLE(dbPeople)
  13.     dbChar        LastName, OtherNames, DepartmentCode, ProjectCode;
  14.     dbLong        Salary;
  15.     dbCompoundField    CombinedNames, NameAndSalary, JobCode;
  16.     dbUshort    StartedYear;
  17.     
  18.  
  19.     dbPeople() :
  20.                 dbTable("People"),
  21.                 LastName(39, "Last Name"),
  22.                 OtherNames(79, "Other Names", kIndexCompress),
  23.                 DepartmentCode(3, "Dept. Code"), 
  24.                 ProjectCode(4, "Proj. Code"),
  25.                 Salary("Salary"),
  26.                 CombinedNames("Combined Names"),
  27.                 NameAndSalary("Name & Salary"),
  28.                 JobCode("Job Code"),
  29.                 StartedYear("Started Year")
  30.                 
  31. {
  32.     CombinedNames << LastName << OtherNames;
  33.     CombinedNames.index(kIndexCompress);  // order of these two lines is not significant
  34.     
  35.     NameAndSalary << LastName >> Salary;
  36.     NameAndSalary.index(kIndexCompress);  
  37.     
  38.     JobCode << DepartmentCode << ProjectCode << StartedYear;
  39.     JobCode.index(kIndexed);
  40. };
  41.     
  42. // my own data entry procedures
  43.     void Add(const char *lname, const char *oname, const long salary, const char *dept, const char *proj, const unsigned short year);
  44. };
  45.  
  46. void dbPeople::Add(const char *lname, const char *oname, const long salary, const char *dept, const char *proj, const unsigned short year)
  47. {
  48.     newRecord();
  49.     LastName = lname;
  50.     OtherNames = oname;
  51.     Salary = salary;
  52.     DepartmentCode = dept;
  53.     ProjectCode = proj;
  54.     StartedYear = year;
  55.     saveRecord();
  56. }
  57.  
  58.  
  59.  
  60. int main()
  61. {
  62.     cout << "OOFILE Validation Suite - Test 12\n"
  63.          << "Simple test to build compound indexes\n"
  64.          << "and demonstrate sorting and searching by them and their components\n";
  65.     
  66.     dbConnect_ctree    theDB;
  67.     dbPeople     People;
  68.     
  69.     if (dbConnect::fileExists("ooftst12.db"))
  70.         theDB.openConnection("ooftst12.db");
  71.     else {
  72.         theDB.newConnection("ooftst12.db");
  73.         People.Add("Smith", "John", 0, "Acc", "XXX", 1970);  // specifically to test zero searches
  74.         People.Add("DENT", "Trissa", 5000, "Acc", "Rec", 1985);
  75.         People.Add("Dent", "Andy", 25000, "Acc", "XXX", 1992);
  76.         People.Add("Dent", "Andrew", 40000, "MIS", "Boss", 1982);  // should sort before Andy on CombinedNames but after on Salary
  77.         People.Add("Denton", "Andrew", 39000, "Pur", "XXX", 1994);  
  78.         People.Add("Taylor", "Ken", 75000, "MIS", "Cntr", 1994);
  79.     }
  80.     
  81.     People.selectAll();
  82.     People.setSortOrder(People.CombinedNames);
  83.     cout << "Listing records by combined names\n" << People << endl;
  84.  
  85.     People.setSortOrder(People.NameAndSalary);
  86.     cout << "Listing records by Name and reverse Salary\n" << People << endl;
  87.  
  88.     People.setSortOrder(People.OtherNames);
  89.     cout << "Listing records in OtherNames order\n" << People << endl;
  90.  
  91. /* BROKEN in v1.1.1 - reuse of compound index by single non-indexed fields
  92.     People.search(People.LastName=="Dent");  // uses compound index
  93.     cout << "Listing three Dent records: " << endl << People << endl;
  94.  
  95.     People.search(People.JobCode.startsWith("AccXXX"));  // uses compound index in direct query
  96.     cout << "Listing two AccXXX records: " << endl << People << endl;
  97. */
  98.  
  99.     People[People.LastName=="Dent" && People.Salary==25000];
  100.     cout << "Listing Andy Dent by compound key search of LastName & Salary: " << endl << People << endl;
  101.  
  102.     People[People.LastName=="Dent" && People.OtherNames=="Andrew"];
  103.     cout << "Listing Andrew Dent by compound key search on both names: " << endl << People << endl;
  104.  
  105. // combinatorial search
  106.     People[ (People.LastName=="Dent" && People.Salary==25000) 
  107.             | People.OtherNames=="Andrew"
  108.     ];
  109.     cout << "Listing Andy Dent & Denton: " << endl << People << endl;
  110.  
  111.     cout << "Test Getting copy from read-only compound field" << endl;
  112.     People.start();
  113.     char* str = People.JobCode.copyAsChars();
  114.     cout << "JobCode is: " << str << endl;
  115.     delete[] str;
  116.  
  117.     dbPeople People2(People);
  118.     People2.start();  // this or a search is ***essential*** after cloning a selection
  119.     cout << endl << "Repeat above read-only compound field test on cloned selection" << endl;
  120.     cout << "JobCode is: " << People2.JobCode << endl;
  121.  
  122.     cout << endl << "Test Completed" << endl;
  123.     
  124.     return EXIT_SUCCESS;